package cn.bsb.controller.backstage;
 
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;

import cn.bsb.pojo.Article;
import cn.bsb.pojo.BackendUser;
import cn.bsb.pojo.DevUser;
import cn.bsb.pojo.Recommend;
import cn.bsb.pojo.Thematic;
import cn.bsb.service.backstage.ArticleService;
import cn.bsb.service.backstage.DevUserService;
import cn.bsb.service.backstage.RecommendService;
import cn.bsb.service.backstage.ThematicService;
import cn.bsb.tools.Constants;

@Controller
@RequestMapping("recommend")
public class RecommendController {

	@Autowired
	private ArticleService article;
	@Autowired
	private DevUserService devUser;
	@Autowired
	private ThematicService thematic;
	@Autowired
	private RecommendService recommend;
	
	 
	 /*
	  * 以推荐文章列表
	  */  
	@RequestMapping("recommendList")
	public String recommendList(Model model,
			@RequestParam(value="pageNum",required=false,defaultValue="1") int pageNum) {
		//以推荐文章分页
		PageInfo<Article> page = recommend.recommend(pageNum, 10);
		model.addAttribute("page", page);
		//专题列表
		List<Thematic> thematicList = thematic.getThematicList();
		model.addAttribute("thematicList", thematicList);
		//用户列表
		List<DevUser> devUserList = devUser.getList();
		model.addAttribute("devUserList", devUserList);
		return "backstage/recommendList";
	}
	@RequestMapping("recommendSelectList")
	public String recommendSelectList(Model model,
			@RequestParam(value="title",required=false) String title) {
		//以推荐文章分页
		List<Article> listarticle =recommend.ReTitle(title);
		model.addAttribute("listarticle", listarticle);
		//专题列表
		List<Thematic> thematicList = thematic.getThematicList();
		model.addAttribute("thematicList", thematicList);
		//用户列表
		List<DevUser> devUserList = devUser.getList();
		model.addAttribute("devUserList", devUserList);
		return "backstage/recommendList";
	}
	
	/*
	  * 推荐文章列表
	  */
	@RequestMapping("goodList")
	public String goodList(Model model,
			HttpSession session,HttpServletRequest request,
			@RequestParam(value="pageNum",required=false,defaultValue="1") int pageNum) {
		//文章分页
		PageInfo<Article> page = recommend.good(pageNum, 10);
		model.addAttribute("page", page);
		//专题列表
		List<Thematic> thematicList = thematic.getThematicList();
		model.addAttribute("thematicList", thematicList);
		//用户列表
		List<DevUser> devUserList = devUser.getList();
		model.addAttribute("devUserList", devUserList);
		return "backstage/goodList";
		
	}
	@RequestMapping("TitleList")
	public String TitleList(Model model,
			@RequestParam(value="title",required=false) String title) {
		//文章分页
		List<Article> listarticle =recommend.Title(title);
		model.addAttribute("listarticle", listarticle);
		//专题列表
		List<Thematic> thematicList = thematic.getThematicList();
		model.addAttribute("thematicList", thematicList);
		//用户列表
		List<DevUser> devUserList = devUser.getList();
		model.addAttribute("devUserList", devUserList);
		return "backstage/goodList";
		
	}
	
	/*
	 * 推荐文章
	 */
	@RequestMapping("add/{id}")
	public String add(@PathVariable(value="id",required=false) Long id,HttpSession session,Model model) {
		BackendUser user = (BackendUser) session.getAttribute(Constants.admin_SESSION);
		Article article1 = article.getOne(id);
		Recommend recommend1 = new Recommend();
		//作者id
		recommend1.setDev_user_id(article1.getDev_user_id());
		//文章id
		recommend1.setArticle_id(article1.getId());
		//推荐者id
		recommend1.setBackend_user_id(user.getId());
		//推荐时间
		recommend1.setTime(new Date());
		if(recommend.addrecommend(recommend1.getDev_user_id(),recommend1.getArticle_id(),recommend1.getBackend_user_id(),recommend1.getTime())) {
			return "redirect:/recommend/goodList";
		}
		model.addAttribute("error","推荐失败");
		return "redirect:/recommend/goodList";
	}
	
	/*
	 * 取消推荐文章
	 */
	@RequestMapping("deleteRe/{id}")
	public String deleteRe(@PathVariable(value="id",required=false) Long id,HttpSession session,Model model) {
		if(recommend.deleteRe(id)) {
			return "redirect:/recommend/recommendList";
		}
		model.addAttribute("error","取消推荐失败");
		return "redirect:/recommend/goodList";
	}
}
